﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.ComponentModel.DataAnnotations;

namespace YodaMaster.Models
{
    public class TimeTable
    {
        [Key]
        public int RecordID {get; set; }
        public DataTable timetable { get; set; }
        public TableModel model {get; set; }
        public int flag { get; set; }
        public List<int> array { get; set; }

        public  TimeTable()
        {
            model = new TableModel();
           
            timetable = new DataTable();
            timetable.Columns.Add("Hour/Day", typeof(string));
            timetable.Columns.Add("Sunday", typeof(string));
            timetable.Columns.Add("Monday", typeof(string));
            timetable.Columns.Add("Tuesday", typeof(string));
            timetable.Columns.Add("Wednesday", typeof(string));
            timetable.Columns.Add("Thursday", typeof(string));
            timetable.Columns.Add("Friday", typeof(string));
            timetable.Rows.Add("8:00", "", "", "", "", "", "");
            timetable.Rows.Add("8:30", "", "", "", "", "", "");
            timetable.Rows.Add("9:00", "", "", "", "", "", "");
            timetable.Rows.Add("9:30", "", "", "", "", "", "");
            timetable.Rows.Add("10:00", "", "", "", "", "", "");
            timetable.Rows.Add("10:30", "", "", "", "", "", "");
            timetable.Rows.Add("11:00", "", "", "", "", "", "");
            timetable.Rows.Add("11:30", "", "", "", "", "", "");
            timetable.Rows.Add("12:00", "", "", "", "", "", "");
            timetable.Rows.Add("12:30", "", "", "", "", "", "");
            timetable.Rows.Add("13:00", "", "", "", "", "", "");
            timetable.Rows.Add("13:30", "", "", "", "", "", "");
            timetable.Rows.Add("14:00", "", "", "", "", "", "");
            timetable.Rows.Add("14:30", "", "", "", "", "", "");
            timetable.Rows.Add("15:00", "", "", "", "", "", "");
            timetable.Rows.Add("15:30", "", "", "", "", "", "");
            timetable.Rows.Add("16:00", "", "", "", "", "", "");
            timetable.Rows.Add("16:30", "", "", "", "", "", "");
            timetable.Rows.Add("17:00", "", "", "", "", "", "");
            timetable.Rows.Add("17:30", "", "", "", "", "", "");
            timetable.Rows.Add("18:00", "", "", "", "", "", "");
            timetable.Rows.Add("18:30", "", "", "", "", "", "");
            timetable.Rows.Add("19:00", "", "", "", "", "", "");
            timetable.Rows.Add("19:30", "", "", "", "", "", "");
            timetable.Rows.Add("20:00", "", "", "", "", "", "");
            timetable.Rows.Add("20:30", "", "", "", "", "", "");
            timetable.Rows.Add("21:00", "", "", "", "", "", "");
            timetable.Rows.Add("21:30", "", "", "", "", "", "");
            timetable.Rows.Add("22:00", "", "", "", "", "", "");
            timetable.Rows.Add("22:30", "", "", "", "", "", "");
        }

        public void AddTeachingGroup(Group_Course group)
        {
            

            foreach (TeachingHour item in group.Group.teachingHours)
            {
                model.time_start = item.startTime;
                model.time_end = item.endTime;
                ConvertDatetime funcs = new ConvertDatetime();
                for (int i = 0; i < 30; i++)
                {
                    model.sunday[i] = 0;
                    model.monday[i] = 0;
                    model.tuesday[i] = 0;
                    model.wednesday[i] = 0;
                    model.thursday[i] = 0;
                    model.friday[i] = 0;
                }
                funcs.Convert(model);
                if (item.startTime.Day == 1)
                {
                    for (int i = 0; i < 30; i++)
                    {
                        if (model.sunday[i] == 1)
                        {
                            string value = group.course.name + " " + group.Group.groupType.ToString() + Environment.NewLine + group.Group.groupNumber + " " + item.location;
                            DataColumn col = timetable.Columns[1];
                            if (timetable.Rows[i][col].ToString() == "")
                                timetable.Rows[i].SetField("Sunday", value);
                            else
                            {
                                string oldvalue = timetable.Rows[i][col].ToString();
                                string newvalue = oldvalue + Environment.NewLine + "@" + Environment.NewLine + value;
                                timetable.Rows[i].SetField("Sunday", newvalue);
                            }
                            

                            
                        }
                        
                    }
               

                }
                if (item.startTime.Day == 2)
                {
                    for (int i = 0; i < 30; i++)
                    {
                        if (model.monday[i] == 1)
                        {
                            string value = group.course.name + " " + group.Group.groupType.ToString() + Environment.NewLine + group.Group.groupNumber + " " + item.location;
                            DataColumn col = timetable.Columns[2];
                            if (timetable.Rows[i][col].ToString() == "")
                                timetable.Rows[i].SetField("Monday", value);
                            else
                            {
                                string oldvalue = timetable.Rows[i][col].ToString();
                                string newvalue = oldvalue + Environment.NewLine + "@" + Environment.NewLine + value;
                                timetable.Rows[i].SetField("Monday", newvalue);
                            }
                            
                       


                        }

                    }
                  
                }
                if (item.startTime.Day == 3)
                {
                    for (int i = 0; i < 30; i++)
                    {
                        if (model.tuesday[i] == 1)
                        {
                            string value = group.course.name + " " + group.Group.groupType.ToString() + Environment.NewLine + group.Group.groupNumber + " " + item.location;
                            DataColumn col = timetable.Columns[3];
                            if (timetable.Rows[i][col].ToString() == "")
                                timetable.Rows[i].SetField("Tuesday", value);
                            else
                            {
                                string oldvalue = timetable.Rows[i][col].ToString();
                                string newvalue = oldvalue + Environment.NewLine + "@" + Environment.NewLine + value;
                                timetable.Rows[i].SetField("Tuesday", newvalue);
                            }
                        


                        }

                    }
                 
                }
                if (item.startTime.Day == 4)
                {
                    for (int i = 0; i < 30; i++)
                    {
                        if (model.wednesday[i] == 1)
                        {
                            string value = group.course.name + " " + group.Group.groupType.ToString() + Environment.NewLine + group.Group.groupNumber + " " + item.location;
                            DataColumn col = timetable.Columns[4];
                            if (timetable.Rows[i][col].ToString() == "")
                                timetable.Rows[i].SetField("Wednesday", value);
                            else
                            {
                                string oldvalue = timetable.Rows[i][col].ToString();
                                string newvalue = oldvalue + Environment.NewLine + "@" + Environment.NewLine + value;
                                timetable.Rows[i].SetField("Wednesday", newvalue);
                            }
                        

                        }
                    }
                   
                }
                if (item.startTime.Day == 5)
                {
                    for (int i = 0; i < 30; i++)
                    {
                        if (model.thursday[i] == 1)
                        {
                            string value = group.course.name + " " + group.Group.groupType.ToString() + Environment.NewLine + group.Group.groupNumber + " " + item.location;
                            DataColumn col = timetable.Columns[5];
                            if (timetable.Rows[i][col].ToString() == "")
                                timetable.Rows[i].SetField("Thursday", value);
                            else
                            {
                                string oldvalue = timetable.Rows[i][col].ToString();
                                string newvalue = oldvalue + Environment.NewLine + "@" + Environment.NewLine + value;
                                timetable.Rows[i].SetField("Thursday", newvalue);
                            }
                        


                        }

                    }
                    
                }
                if (item.startTime.Day == 6)
                {
                    for (int i = 0; i < 30; i++)
                    {
                        if (model.friday[i] == 1)
                        {
                            string value = group.course.name + " " + group.Group.groupType.ToString() + Environment.NewLine + group.Group.groupNumber + " " + item.location;
                            DataColumn col = timetable.Columns[6];
                            if (timetable.Rows[i][col].ToString() == "")
                                timetable.Rows[i].SetField("Friday", value);
                            else
                            {
                                string oldvalue = timetable.Rows[i][col].ToString();
                                string newvalue = oldvalue + Environment.NewLine + "@" + Environment.NewLine + value;
                                timetable.Rows[i].SetField("Friday", newvalue);
                            }



                        }
                    }
                  }
                }  

            }

                    
        
        public void RemoveTeachingGroup(Group_Course group)
        {
            foreach (TeachingHour item in group.Group.teachingHours)
            {
                model.time_start = item.startTime;
                model.time_end = item.endTime;
                ConvertDatetime funcs = new ConvertDatetime();
                funcs.Convert(model);
                if (item.startTime.Day == 1)
                {
                    for (int i = 0; i < 30; i++)
                    {
                        if (model.sunday[i] == 1)
                        {
                            timetable.Rows[i].SetField("Sunday","");
                            model.sunday[i] = 0;
                        }
                    }

                }
                if (item.startTime.Day == 2)
                {
                    for (int i = 0; i < 30; i++)
                    {
                        if (model.monday[i] == 1)
                        {
                            timetable.Rows[i].SetField("Monday", "");
                            model.monday[i] = 0;
                        }
                    }
                }
                if (item.startTime.Day == 3)
                {
                    for (int i = 0; i < 30; i++)
                    {
                        if (model.tuesday[i] == 1)
                        {
                            timetable.Rows[i].SetField("Tuesday", "");
                            model.tuesday[i] = 0;
                        }
                    }
                }
                if (item.startTime.Day == 4)
                {
                    for (int i = 0; i < 30; i++)
                    {
                        if (model.wednesday[i] == 1)
                        {
                           timetable.Rows[i].SetField("Wednesday", "");
                           model.wednesday[i] = 0;
                        }
                    }
                }
                if (item.startTime.Day == 5)
                {
                    for (int i = 0; i < 30; i++)
                    {
                        if (model.thursday[i] == 1)
                        {
                           timetable.Rows[i].SetField("Thursday", "");
                        }
                    }
                }
                if (item.startTime.Day == 6)
                {
                    for (int i = 0; i < 30; i++)
                    {
                        if (model.friday[i] == 1)
                        {
                            timetable.Rows[i].SetField("Friday", "");
                        }
                    }
                }
            }
        }
        public void EmptyTable()
        {
        }
    

    }
}